package com.regex.web.rest.kf;


import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.regex.web.utils.QueryResult;
import com.regex.web.entity.kf.ClCjrjkxxDTO;
import com.regex.web.service.kf.IClCjrjkxxService;
import com.regex.web.utils.Contant;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.dynamic.datasource.annotation.DS;

import com.regex.web.utils.TransData;
import com.regex.web.service.BaseService;

/**
 * <p>
 * 残疾人康复信息表 rest 接口
 * </p>
 *
 * @author wbj
 * @since 2021-06-18
 */
@Controller
@Component
@Transactional

public class ClCjrjkxxRestController {

	private final static Logger logger = LoggerFactory.getLogger(ClCjrjkxxRestController.class);
	
	@Autowired
    private BaseService baseService;
	
	@Autowired
	private IClCjrjkxxService IClCjrjkxxService;
	
	/**
	 * 分页查询
	 * @param page
	 * @param model
	 * @param clCjrjkxxDTO
	 * @return
	 */
	 public TransData<QueryResult<ClCjrjkxxDTO>> querycl_cjrjkxxData(TransData<QueryResult<ClCjrjkxxDTO>> transData, HttpServletRequest request,
            HttpServletResponse response) {
		try {
            if (transData.getO() == TransData.SELECT) {// 查询
            	if (StringUtils.isNotBlank(TransData.terminal.get(transData.getT()))) {
            		Map<String, Object> param = baseService.washParams(transData.getM());
            		String token = (String) param.get("token");
            		String pageNumber = (String) param.get("pageNumber");
                	String pageSize = (String) param.get("pageSize");
            		String nameStr = (String) param.get("name");
            		String ageStr = (String) param.get("age");
            		String codeStr = (String) param.get("code");
            		String cjlxStr = (String) param.get("cjlx");
            		String cjdjStr = (String) param.get("cjdj");
            		String fwnrStr = (String) param.get("fwnr");
            		String kfjgStr = (String) param.get("kfjg");
            		String fwkssjStr = (String) param.get("fwkssj");
            		String fwjssjStr = (String) param.get("fwjssj");
            		String kfztStr = (String) param.get("kfzt");
            		
            		if (StringUtils.isEmpty(token)) {
	                    transData.setG(TransData.TOKEN_OVERTIME);
	                    transData.setA(TransData.errors.get(TransData.TOKEN_OVERTIME));
	                    return transData;
	                }
            		
					QueryWrapper<ClCjrjkxxDTO> queryWrapper = new QueryWrapper<ClCjrjkxxDTO>();
	            		if(StringUtils.isNotEmpty(nameStr)) {
	            			queryWrapper.eq("name", nameStr);
	            		}
	            		if(StringUtils.isNotEmpty(ageStr)) {
	            			queryWrapper.eq("age", ageStr);
	            		}
	            		if(StringUtils.isNotEmpty(codeStr)) {
	            			queryWrapper.eq("code", codeStr);
	            		}
	            		if(StringUtils.isNotEmpty(cjlxStr)) {
	            			queryWrapper.eq("cjlx", cjlxStr);
	            		}
	            		if(StringUtils.isNotEmpty(cjdjStr)) {
	            			queryWrapper.eq("cjdj", cjdjStr);
	            		}
	            		if(StringUtils.isNotEmpty(fwnrStr)) {
	            			queryWrapper.eq("fwnr", fwnrStr);
	            		}
	            		if(StringUtils.isNotEmpty(kfjgStr)) {
	            			queryWrapper.eq("kfjg", kfjgStr);
	            		}
	            		if(StringUtils.isNotEmpty(fwkssjStr)) {
	            			queryWrapper.eq("fwkssj", fwkssjStr);
	            		}
	            		if(StringUtils.isNotEmpty(fwjssjStr)) {
	            			queryWrapper.eq("fwjssj", fwjssjStr);
	            		}
	            		if(StringUtils.isNotEmpty(kfztStr)) {
	            			queryWrapper.eq("kfzt", kfztStr);
	            		}
            		Page<ClCjrjkxxDTO> page = new Page<ClCjrjkxxDTO>();
            		if(StringUtils.isEmpty(pageNumber)) {
            			pageNumber = "1";
            		}
            		if(StringUtils.isEmpty(pageSize)) {
            			pageSize = "50";
            		}
	                page.setCurrent(Integer.parseInt(pageNumber));
	                page.setSize(Integer.parseInt(pageSize));
			        QueryResult<ClCjrjkxxDTO> result = IClCjrjkxxService.page(page, queryWrapper);
	        		
	        		if(result != null && result.getDatas() != null && result.getDatas().size() > 0) {
	    	        	transData.setData(result);
	    	        	transData.setG(TransData.SUCCESS);
	    	        	transData.setA(TransData.errors.get(TransData.SUCCESS));
	    	        } else {
	    	        	transData.setG(TransData.EMPTY_RECORD);
	    	        	transData.setA(TransData.errors.get(TransData.EMPTY_RECORD));
	    	        }
	        } else {
            		transData.setG(TransData.PARAMS_ERROR);
                    transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            	}
            } else {
                transData.setG(TransData.PARAMS_ERROR);
                transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            }
        } catch (Exception e) {
			e.printStackTrace();
			transData.setG(TransData.SYSTEM_ERROR);
            transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
			logger.error("show page is Exception!");
		}
		return transData;
	}
	
	/**
	* 保存数据  新增/ 修改
	**/
	public TransData<ClCjrjkxxDTO> savecl_cjrjkxxData(TransData<ClCjrjkxxDTO> transData, HttpServletRequest request,
            HttpServletResponse response) {
		try {
            if (transData.getO() == TransData.ADD || transData.getO() == TransData.UPDATE) {// 新增 /修改
            	if (StringUtils.isNotBlank(TransData.terminal.get(transData.getT()))) {
            		Map<String, Object> param = baseService.washParams(transData.getM());
            		// String token = (String) param.get("token");
            		String id = (String) param.get("id");
            		// 获取新增参数
            			String name = (String) param.get("name");
            			Integer age = (Integer) param.get("age");
            			String code = (String) param.get("code");
            			String cjlx = (String) param.get("cjlx");
            			String cjdj = (String) param.get("cjdj");
            			String fwnr = (String) param.get("fwnr");
            			String kfjg = (String) param.get("kfjg");
            			String fwkssj = (String) param.get("fwkssj");
            			String fwjssj = (String) param.get("fwjssj");
            			String kfzt = (String) param.get("kfzt");
            		
            		// 检查参数
            		
            		boolean flag = false;
            		ClCjrjkxxDTO clCjrjkxxDTO = null;
            		// 修改
            		
            		if(id != null) {
		        		clCjrjkxxDTO = IClCjrjkxxService.getById(id);
		        		
		        		if(clCjrjkxxDTO == null) {
		        			transData.setG(TransData.PARAMS_ERROR);
	                    	transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
	                    	return transData;
		        		}
		        		
			        			clCjrjkxxDTO.setName(name);
			        			clCjrjkxxDTO.setAge(age);
			        			clCjrjkxxDTO.setCode(code);
			        			clCjrjkxxDTO.setCjlx(cjlx);
			        			clCjrjkxxDTO.setCjdj(cjdj);
			        			clCjrjkxxDTO.setFwnr(fwnr);
			        			clCjrjkxxDTO.setKfjg(kfjg);
			        			clCjrjkxxDTO.setFwkssj(fwkssj);
			        			clCjrjkxxDTO.setFwjssj(fwjssj);
			        			clCjrjkxxDTO.setKfzt(kfzt);
		        		
		        		flag = IClCjrjkxxService.updateById(clCjrjkxxDTO);
            		} else {
            		//  新增
            		clCjrjkxxDTO = new ClCjrjkxxDTO();
            		
		        			clCjrjkxxDTO.setName(name);
		        			clCjrjkxxDTO.setAge(age);
		        			clCjrjkxxDTO.setCode(code);
		        			clCjrjkxxDTO.setCjlx(cjlx);
		        			clCjrjkxxDTO.setCjdj(cjdj);
		        			clCjrjkxxDTO.setFwnr(fwnr);
		        			clCjrjkxxDTO.setKfjg(kfjg);
		        			clCjrjkxxDTO.setFwkssj(fwkssj);
		        			clCjrjkxxDTO.setFwjssj(fwjssj);
		        			clCjrjkxxDTO.setKfzt(kfzt);
	        		flag = IClCjrjkxxService.save(clCjrjkxxDTO);
            		}
            		
            		if(flag) {
            			transData.setData(clCjrjkxxDTO);
	    	        	transData.setG(TransData.SUCCESS);
	    	        	transData.setA(TransData.errors.get(TransData.SUCCESS));
            		} else {
            			transData.setG(TransData.SYSTEM_ERROR);
            			transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
            		}
        		} else {
            		transData.setG(TransData.PARAMS_ERROR);
                    transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            	}
            } else {
                transData.setG(TransData.PARAMS_ERROR);
                transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            }
        } catch (Exception e) {
			e.printStackTrace();
			transData.setG(TransData.SYSTEM_ERROR);
            transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
			logger.error("save is Exception!");
		}
		return transData;
	}
    
	
	/**
	 * 详情
	 * @param id
	 * @return
	 */
    public TransData<ClCjrjkxxDTO> getcl_cjrjkxxDetialData(TransData<ClCjrjkxxDTO> transData, HttpServletRequest request,
            HttpServletResponse response) {
		try {
            if (transData.getO() == TransData.SELECT) {// 获取详情
            	if (StringUtils.isNotBlank(TransData.terminal.get(transData.getT()))) {
            		Map<String, Object> param = baseService.washParams(transData.getM());
            		String id = (String) param.get("id");
            		
			        if(StringUtils.isEmpty(id)) {
			        	transData.setG(TransData.PARAMS_ERROR);
                		transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
                		return transData;
			        }
			        
		        	ClCjrjkxxDTO clCjrjkxxDTO = IClCjrjkxxService.getById(id);
			        
			        transData.setData(clCjrjkxxDTO);
    	        	transData.setG(TransData.SUCCESS);
    	        	transData.setA(TransData.errors.get(TransData.SUCCESS));
        		} else {
            		transData.setG(TransData.PARAMS_ERROR);
                    transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            	}
            } else {
                transData.setG(TransData.PARAMS_ERROR);
                transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            }
        } catch (Exception e) {
			e.printStackTrace();
			transData.setG(TransData.SYSTEM_ERROR);
            transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
			logger.error("save is Exception!");
		}
		return transData;
	}
	
	/**
	 * 删除
	 * @param id
	 * @param response
	 */
	public TransData<String> deletecl_cjrjkxxData(TransData<String> transData, HttpServletRequest request,
            HttpServletResponse response) {
		try {
            if (transData.getO() == TransData.DELETE) {// 删除
            	if (StringUtils.isNotBlank(TransData.terminal.get(transData.getT()))) {
            		Map<String, Object> param = baseService.washParams(transData.getM());
            		String id = (String) param.get("id");
            		
			        if(StringUtils.isEmpty(id)) {
			        	transData.setG(TransData.PARAMS_ERROR);
                		transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
                		return transData;
			        }
			        
		        	boolean flag = IClCjrjkxxService.removeById(id);
			        
			        if(flag) {
				        transData.setData(id);
	    	        	transData.setG(TransData.SUCCESS);
	    	        	transData.setA(TransData.errors.get(TransData.SUCCESS));
			        } else {
			        	transData.setG(TransData.SYSTEM_ERROR);
            			transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
			        }
        		} else {
            		transData.setG(TransData.PARAMS_ERROR);
                    transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            	}
            } else {
                transData.setG(TransData.PARAMS_ERROR);
                transData.setA(TransData.errors.get(TransData.PARAMS_ERROR));
            }
        } catch (Exception e) {
			e.printStackTrace();
			transData.setG(TransData.SYSTEM_ERROR);
            transData.setA(TransData.errors.get(TransData.SYSTEM_ERROR));
			logger.error("delete is Exception!");
		}
		return transData;
	}
    
    
	
}
